package com.weizhizs.assistant.code.helper;

import com.weizhizs.assistant.code.vo.ColumnVO;
import com.weizhizs.assistant.utils.ArrayUtils;
import com.weizhizs.assistant.utils.CharUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wutianbin
 * @version 1.0
 * @Time: 2017年5月18日 下午10:19:00
 */
public class TableHelper {

    public static String tableNameToClassName(String tableName) {
        char[] ch = tableName.toCharArray();

        ch[0] = CharUtils.toUpperChar(ch[0]);

        StringBuffer sb = new StringBuffer();

        boolean toUpper = false;

        for (char c : ch) {
            if (c == '_') {
                toUpper = true;
            } else {
                if (toUpper) {
                    c = CharUtils.toUpperChar(c);
                    toUpper = false;
                }

                sb.append(c);
            }
        }

        return sb.toString();
    }

    public static String columnNameToAttrName(String columnName) {
        char[] ch = columnName.toCharArray();

        ch[0] = CharUtils.toLowerChar(ch[0]);

        StringBuffer sb = new StringBuffer();

        boolean toUpper = false;

        for (char c : ch) {
            if (c == '_') {
                toUpper = true;
            } else {
                if (toUpper) {
                    c = CharUtils.toUpperChar(c);
                    toUpper = false;
                }

                sb.append(c);
            }
        }

        return sb.toString();
    }

    public static boolean colTypeIsString(String javaType) {
        if (null == javaType) {
            return false;
        }

        if (javaType.equalsIgnoreCase("string")) {
            return true;
        }

        return false;
    }

    /**
     * 功能：获得列的数据类型
     *
     * @param sqlType
     * @return
     */
    public static String sqlTypeToJavaType(String sqlType) {
        if (sqlType.equalsIgnoreCase("bit")) {
            return "Boolean";
        } else if (sqlType.equalsIgnoreCase("year")) {
            return "Integer";
        } else if (sqlType.equalsIgnoreCase("tinyint")) {
            return "Integer";
        } else if (sqlType.equalsIgnoreCase("smallint")) {
            return "Integer";
        } else if (sqlType.equalsIgnoreCase("int")) {
            return "Integer";
        } else if (sqlType.equalsIgnoreCase("bigint")) {
            return "Long";
        } else if (sqlType.equalsIgnoreCase("float")) {
            return "Float";
        } else if (sqlType.equalsIgnoreCase("decimal") ||
                sqlType.equalsIgnoreCase("numeric") ||
                sqlType.equalsIgnoreCase("real") ||
                sqlType.equalsIgnoreCase("money") ||
                sqlType.equalsIgnoreCase("smallmoney") ||
                sqlType.equalsIgnoreCase("double")) {
            return "Double";
        } else if (sqlType.equalsIgnoreCase("varchar") ||
                sqlType.equalsIgnoreCase("char") ||
                sqlType.equalsIgnoreCase("nvarchar") ||
                sqlType.equalsIgnoreCase("nchar") ||
                sqlType.equalsIgnoreCase("text") ||
                sqlType.equalsIgnoreCase("json") ||
                sqlType.equalsIgnoreCase("longtext")) {
            return "String";
        } else if (sqlType.equalsIgnoreCase("datetime") ||
                sqlType.equalsIgnoreCase("timestamp")) {
            return "LocalDateTime";
        } else if (sqlType.equalsIgnoreCase("date")) {
            return "LocalDate";
        } else if (sqlType.equalsIgnoreCase("time")) {
            return "LocalTime";
        } else if (sqlType.equalsIgnoreCase("image") ||
                sqlType.equalsIgnoreCase("blob") ||
                sqlType.equalsIgnoreCase("longbolb") ||
                sqlType.equalsIgnoreCase("mediumttext")) {
            return "Blob";
        }

        return null;
    }

    public static String moduleFromTableName(String tableName) {
        int pos = tableName.indexOf('_');
        if (pos > 0) {
            return tableName.substring(0, pos).toLowerCase();
        } else if (pos == 0) {
            return tableName.substring(1).toLowerCase();
        } else {
            return tableName.toLowerCase();
        }
    }

    public static String filepathFromTableName(String tableName) {
        int pos = tableName.indexOf('_');
        String ret = tableName;
        if (pos > 0) {
            ret = tableName.substring(pos + 1).toLowerCase();
        } else if (pos == 0) {
            ret = tableName.substring(1).toLowerCase();
        } else {
            ret = tableName.toLowerCase();
        }
        return ret;
    }

    public static String pathFromTableName(String tableName) {
        int pos = tableName.indexOf('_');
        String ret = tableName;
        if (pos > 0) {
            ret = tableName.substring(pos + 1).toLowerCase();
        } else if (pos == 0) {
            ret = tableName.substring(1).toLowerCase();
        } else {
            ret = tableName.toLowerCase();
        }
        return columnNameToAttrName(ret);
    }

    public static List<ColumnVO> getRealColumns(List<ColumnVO> columns, String[] filterFields) {
        List<ColumnVO> ret = new ArrayList<>();
        for (ColumnVO col : columns) {
            if (ArrayUtils.contains(filterFields, col.getAttrName())) {
                continue;
            } else {
                ret.add(col);
            }
        }
        return ret;
    }

    public static String getRequestPath(String entityObject) {
        char[] ch = entityObject.toCharArray();

        StringBuilder pre = new StringBuilder();
        StringBuilder sub = new StringBuilder();

        boolean toUpper = false;

        for (char c : ch) {
            if (!toUpper && CharUtils.isUpperChar(c)) {
                toUpper = true;
                c = CharUtils.toLowerChar(c);
            }

            if (toUpper) {
                sub.append(c);
            } else {
                pre.append(c);
            }
        }

        return pre.toString() + "/" + sub.toString();
    }

}
